package com.leyou.service.activity;

import com.leyou.api.ActivityService;
import com.leyou.constant.DefineException;
import com.leyou.dao.CouponactivityMapper;
import com.leyou.dao.CouponrecordMapper;
import com.leyou.dao.LogMapper;
import com.leyou.dao.ManagerMapper;
import com.leyou.db2dao.RelateLeyouCouponsMapper;
import com.leyou.domain.db1entity.*;
import com.leyou.domain.vo.*;
import com.leyou.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class ActivityServiceImp implements ActivityService {

    @Autowired
    private ManagerMapper managerMapper;


    @Autowired
    private CouponactivityMapper couponactivityMapper;


    @Autowired
    private LogMapper logMapper;


    @Autowired
    private RelateLeyouCouponsMapper relateLeyouCouponsMapper;


    @Autowired
    private CouponrecordMapper couponrecordMapper;

    @Override
    public int checkUser(Manager manager) {
        return managerMapper.selectManager(manager);
    }

    @Override
    public List<CouponActivityGetVO> getCouponActivityList(Integer pageNum, Integer pageSize) {
        Integer pageBegin = (pageNum - 1) * pageSize;
        return couponactivityMapper.getCouponActivityList(pageBegin, pageSize);
    }

    @Override
    public List<Couponactivity> getTotal() {
        return couponactivityMapper.selectByExample(null);
    }

    @Override
    public void putCouponActivity(CouponActivityPutVO couponActivityPutVO) {

        String currentActivityKey = Utils.getUUID();
        int total = 0;

        //日志
        Log log = new Log();
        log.setLogkey(Utils.getUUID());
        log.setActivitytype("1");
        log.setBehaviour("1");
        log.setManagerkey(couponActivityPutVO.getManagerkey());
        int toLog = logMapper.insertSelective(log);

        //生成优惠券活动
        List<Couponandactivity> couponandactivityList = new ArrayList<>();
        for (int i = 0; i < couponActivityPutVO.getCouponsKeyList().size(); i++) {
            Couponandactivity couponandactivity = new Couponandactivity();
            couponandactivity.setCouponandactivitykey(Utils.getUUID());
            couponandactivity.setActivitykey(currentActivityKey);
            couponandactivity.setCouponkey(couponActivityPutVO.getCouponsKeyList().get(i).getCouponkey());
            couponandactivity.setTotal(couponActivityPutVO.getCouponsKeyList().get(i).getTotal());
            couponandactivity.setAmount(couponActivityPutVO.getCouponsKeyList().get(i).getAmount());
            total += couponandactivity.getTotal();
            couponandactivityList.add(couponandactivity);
        }
        int toActivityAndCoupon = couponactivityMapper.putActivityAndCouponList(couponandactivityList);


        couponActivityPutVO.setActivitykey(currentActivityKey);
        couponActivityPutVO.setCount(total);
        couponActivityPutVO.setTotal(total);
        couponActivityPutVO.setManagerkey("admin");
        //活动表
        int toCouponActivity = couponactivityMapper.putCounponActivity(couponActivityPutVO);

        if (toCouponActivity < 1 || toLog < 1 || toActivityAndCoupon < couponandactivityList.size()) {
            throw new DefineException(900007, "插入失败");
        }

    }

    @Override
    public void checkCoupon(List<Couponandactivity> couponandactivityList) {
        if (relateLeyouCouponsMapper.getCoupon(couponandactivityList) != couponandactivityList.size()) {
            throw new DefineException(400002, "优惠券验证不合法请查证后重试");
        }
    }

    @Override
    public LogVO getLogList(PageVO pageVO) {
        LogVO logVO = new LogVO();
        List<Log> logList = couponactivityMapper.getLogList(pageVO.getPageBegin(), pageVO.getPageSize());
        if (logList != null && logList.size() != 0) {
            logVO.setLogList(logList);
        }
        logVO.setTotal(logMapper.selectByExample(null).size());
        return logVO;
    }

    @Override
    public Couponactivity getActivityById(String activityKey) {
        CouponactivityExample couponactivityExample = new CouponactivityExample();
        couponactivityExample.or().andActivitykeyEqualTo(activityKey);
        int result = couponactivityMapper.selectByExample(couponactivityExample).size();
        if (result != 1) {
            throw new DefineException(400000, "数据异常当前活动查询不到");
        } else {
            return couponactivityMapper.selectByExample(couponactivityExample).get(0);
        }
    }

    @Override
    public void updateActivity(CouponActivityUpdateVO couponActivityUpdateVO,String managername) {

        int updateResult = couponactivityMapper.updateActivityById(couponActivityUpdateVO);

        Log log = new Log();
        log.setLogkey(Utils.getUUID());
        log.setBehaviour("2");
        log.setManagerkey(managername);
        log.setActivitytype("1");
        int addResult = logMapper.insertSelective(log);

        if (updateResult == 0 || addResult == 0) {
            throw new DefineException(400000, "修改活动失败");
        }

    }

    @Override
    public boolean checkReceive(ReceiveVO receiveVO) {
        CouponrecordExample couponrecordExample = new CouponrecordExample();
        couponrecordExample.or().andActivitykeyEqualTo(receiveVO.getActivitykey()).andPhoneEqualTo(receiveVO.getPhone());
        if (couponrecordMapper.selectByExample(couponrecordExample).size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void putEmailLog(Email email) {
        managerMapper.insertEmailLog(email);
    }

    @Override
    public void chekEmail(Email email) {
        if (managerMapper.checkEmail(email) > 0) {
            throw new DefineException(-1, "邮箱已被注册请直接登录！");
        }
    }

    @Override
    public void putManager(Manager manager) {
        managerMapper.insertManager(manager);
    }

    @Override
    public void updateManagerTicket(Manager manager) {
        managerMapper.updateManagerTicket(manager);
    }

    @Override
    public Manager getManagerByTicket(String ticket) {
        return managerMapper.getManagerByTicket(ticket);
    }

    @Override
    public void updateManager(Manager manager) {
        managerMapper.updateManager(manager);
    }


}
